//league.js
// 智能合约地址
import Web3 from "web3";
import TokenCenterJS, {MSGSENDER} from "./TokenCenter"
import ExchangeJS from "@/utils/Exchange";
import ERC20JS from "./ERC20"
import {LeagueABI} from "./contractABI"
import "nprogress/nprogress.css"
import {Message} from 'element-ui';
import fa from "element-ui/src/locale/lang/fa";
// 智能合约ABI
const web3 = new Web3(Web3.givenProvider);
let contract

/**
 *初始化地址
 * @param callback
 */
function loadAddress(callback) {
    TokenCenterJS.getLeagueAddress(address => {
            contract = new web3.eth.Contract(LeagueABI, address);
            callback(true)
        }
    )

}

/**
 * 账号权限检测
 * @param option 账号
 * @param callback 返回值
 */
function accountsPower(option, callback) {
    loadAddress(r => {
        if (r) {
            console.log(MSGSENDER)
            console.log(option)
            let accountStatus = false
            if (option === 0) {
                contract.methods.leagueCommitteePower(MSGSENDER).call()
                    .then(r => {
                        if (r === true) {
                            Message({
                                message: "账号检测完成,即将跳转页面",
                                type: "success",
                                offset: 10
                            })
                            accountStatus = true
                        } else {
                            Message({
                                message: "账号检测完成,您暂时无权限",
                                type: "error",
                                offset: 10
                            })

                        }
                        callback(accountStatus)
                    })
            } else if (option === 1) {
                contract.methods.leagueManagerPower(MSGSENDER).call()
                    .then(r => {
                        if (r === true) {
                            Message({
                                message: "账号检测完成,即将跳转页面",
                                type: "success",
                                offset: 10
                            })
                            accountStatus = true
                        } else {
                            Message({
                                message: "账号检测完成,您暂时无权限",
                                type: "error",
                                offset: 10
                            })
                        }
                        callback(accountStatus)
                    })
            } else {
                contract.methods.leagueMembersPower(MSGSENDER).call()
                    .then(r => {
                        if (r === true) {
                            Message({
                                message: "账号检测完成,即将跳转页面",
                                type: "success",
                                offset: 10
                            })
                            accountStatus = true
                        } else {
                            Message({
                                message: "账号检测完成,您暂时无权限",
                                type: "error",
                                offset: 10
                            })
                        }
                        callback(accountStatus)
                    })
            }

        }
    })

}

/**
 * 获取联盟基本成员信息
 * @param callback
 */
function getLeagueInfo(callback) {
    loadAddress(r => {
        if (r) {
            contract.methods.getConsistMember().call()
                .then(r => callback(r))
        }
    })

}

function getTotalPeople(callback){
    loadAddress(r=>{
        if(r){
            contract.methods.getLeagueTotalPeople().call()
                .then(r=>callback(r))
        }
    })
}

/**
 * Token 预检
 * @param callback
 */
function tokenPreviewing(callback) {
    loadAddress(r => {
        if (r) {
            contract.methods.leagueMembersPower(MSGSENDER).call() //检测是否有权限
                .then(r => {
                    if (r) {
                        contract.methods.memberInfo(MSGSENDER).call() //查询
                            .then(r => {
                                if (r) {
                                    TokenCenterJS.getExchangeAddressByTokenSymbol(r.memberInfoName, status => {
                                        let callStatus
                                        callStatus = status !== "0x0000000000000000000000000000000000000000";
                                        callback(callStatus)
                                    })
                                }
                            })
                    }
                })
        }
    })

}

/**
 * ERC20生成Token
 * @param option Token总量
 * @param callback
 */
function tokenCreating(option, callback) {
    loadAddress(r => {
        if (r) {
            contract.methods.leagueMembersPower(MSGSENDER).call()
                .then(r => {
                    if (r) {
                        contract.methods.memberInfo(MSGSENDER).call() //查询
                            .then(r => {
                                if (r) {
                                    console.log(r)
                                    let data = {
                                        initialSupply: option,
                                        tokenName: r.memberInfoName,
                                        tokenSymbol: r.memberInfoName
                                    }
                                    TokenCenterJS.tokenCreate(data, status => {
                                        console.log(status)
                                        callback(status)
                                    })
                                }
                            })
                    }
                })
        }
    })

}

/**
 * ERC20授权交易
 * @param option 授权金额
 * @param callback 返回值
 */
function tokenApproving(option, callback) {
    loadAddress(r => {
        if (r) {
            TokenCenterJS.getTokenAddressByMSG(tokenAddress => {
                TokenCenterJS.getExchangeAddressByTokenAddress(tokenAddress, ExchangeAddress => {
                    let approveDate = {
                        spender: ExchangeAddress,
                        amount: option
                    }
                    ERC20JS.tokenApprove(tokenAddress, approveDate, status => {
                        callback(status)
                    })
                })
            })
        }
    })

}

/**
 * Token 注入交易池
 * @param option Token amount       Token price
 * @param callback
 */
function tokenPoolPulling(option, callback) {
    loadAddress(r => {
        if (r) {
            TokenCenterJS.getTokenAddressByMSG(tokenAddress => {
                TokenCenterJS.getExchangeAddressByTokenAddress(tokenAddress, ExchangeAddress => {
                    ExchangeJS.tokenIntoPool(ExchangeAddress, option, status => {
                        callback(status)
                    })
                })
            })
        }
    })

}

/**
 * 获取调用者对应的Token 生产等情况
 * @param callback
 */
function getTokenWaysInfo(callback) {
    loadAddress(r => {
        if (r) {
            contract.methods.getMemberInfo(MSGSENDER).call()
                .then(r => {
                    callback(r)
                })
        }
    })
}

function getCarbonFactoryHistory(callback){
    loadAddress(r=>{
        if (r){
            contract.methods.getCarbonToken().call()
                .then(r=>callback(r))
        }
    })
}

/**
 * 获取成员申请记录
 * @param option  申请记录类型   0 全部  1 正在执行
 * @param callback  返回值
 */
function getProposalsInfo(option,callback){
    loadAddress(r=>{
        if (r){
            contract.methods.getProposalsForWorkingInfo(option).call()
                .then(r=>callback(r))
        }
    })
}

function  applyIntoLeague(option,callback){
    loadAddress(r=>{
        if(r){
            contract.methods.applyLeague(option.username,option.contact).send({from:MSGSENDER})
                .on('receipt', function(receipt){
                    callback(true)
                    // receipt 相关例子
                })
                .on('error', function(error, receipt) {
                    // 如果交易被网络拒绝并带有交易收据，则第二个参数将是交易收据。
                })
        }
    })
}

/**
 * 查询可销毁Token量
 * @param callback 返回值
 */
function getTotalResidualBurnByMSG(callback) {
    loadAddress(r => {
        if (r) {
            TokenCenterJS.getTokenAddressByMSG(tokenAddress => {
                    ERC20JS.getTotalResidualBurn(tokenAddress, amount => {
                        callback(amount)
                    })
            })
        }
    })

}

/**
 * ERC20销毁Token
 * @param option 销毁量
 * @param callback
 */
function tokenBurning(option, callback) {
    loadAddress(r => {
        if (r) {
            contract.methods.leagueMembersPower(MSGSENDER).call()
                .then(r => {
                    if (r) {
                        contract.methods.memberInfo(MSGSENDER).call() //查询
                            .then(r => {
                                if (r) {
                                    let data = {
                                        initialSupply: option,
                                        tokenName: r.memberInfoName,
                                        tokenSymbol: r.memberInfoName
                                    }
                                    TokenCenterJS.tokenBurn(data, status => {
                                        console.log(status)
                                        callback(status)
                                    })
                                }
                            })
                    }
                })
        }
    })

}

/**
 * 查看提案同意比例
 * @param callback
 */
function getExecuteRatio(callback){
    loadAddress(r => {
        if (r) {
            contract.methods.confirmationsRatio().call()
                .then(r=>callback(r/100))
        }
    })
}
/**
 * 管理员设置提案同意比例
 * @param option  比例
 * @param callback 返回值
 */
function setExecuteRatioByM(option,callback){
    loadAddress(r => {
        if (r) {
            contract.methods.setExecuteRatio(option).send({from: MSGSENDER})
                .on('receipt', function(receipt){
                    callback(true)
                    // receipt 相关例子
                })
                .on('error', function(error, receipt) {
                    // 如果交易被网络拒绝并带有交易收据，则第二个参数将是交易收据。
                })
        }
    })
}
/**
 * 查看提案审议时间
 * @param callback
 */
function getTimeLimit(callback){
    loadAddress(r => {
        if (r) {
            contract.methods.limitTimeInit().call()
                .then(r=>callback(r))
        }
    })
}
/**
 * 管理员设置提案审议时间
 * @param option  时间
 * @param callback 返回值
 */
function setTimeLimitByM(option,callback){
    loadAddress(r => {
        if (r) {
            contract.methods.setTimeLimit(option).send({from: MSGSENDER})
                .on('receipt', function(receipt){
                    callback(true)
                    // receipt 相关例子
                })
                .on('error', function(error, receipt) {
                    // 如果交易被网络拒绝并带有交易收据，则第二个参数将是交易收据。
                })
        }
    })
}

/**
 * 管理员执行自动清理
 * @param option  时间
 * @param callback 返回值
 */
function autoDelByMSG(option,callback){
    loadAddress(r => {
        if (r) {
            contract.methods.autoReady().send({from:MSGSENDER})
                .on('receipt', function(receipt){
                    callback(true)
                    // receipt 相关例子
                })
                .on('error', function(error, receipt) {
                    // 如果交易被网络拒绝并带有交易收据，则第二个参数将是交易收据。
                })
        }
    })
}

/**
 * 附议提案
 * @param option  提案ID
 * @param callback 返回值
 */
function voteByMSG(option,callback){
    loadAddress(r => {
        if (r) {
            contract.methods.membersVoting(option).send({from:MSGSENDER})
                .on('receipt', function(receipt){
                    callback(true)
                    // receipt 相关例子
                })
                .on('error', function(error, receipt) {
                    // 如果交易被网络拒绝并带有交易收据，则第二个参数将是交易收据。
                })
        }
    })
}
function getAllMemberInfo(callback){
    loadAddress(r=>{
        if(r){
            contract.methods.getAllMembersInfo().call()
                .then(r=>callback(r))
        }
    })
}
export default {
    accountsPower,
    getLeagueInfo,
    tokenPreviewing,
    tokenCreating,
    tokenApproving,
    tokenPoolPulling,
    tokenBurning,
    getTokenWaysInfo,
    getTotalPeople,
    getCarbonFactoryHistory,
    getProposalsInfo,
    applyIntoLeague,
    getTotalResidualBurnByMSG,
    setExecuteRatioByM,
    getExecuteRatio,
    setTimeLimitByM,
    getTimeLimit,
    autoDelByMSG,
    voteByMSG,
    getAllMemberInfo
}
